<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>C++ Tutorial: Multi-Threaded Programming II - Thread for Win32 (B)</title>
  <meta
 content="C++ Tutorial: Multi-Threaded Programming II - Thread for Win32 (B)"
 name="description" />
  <meta
 content="C++ Tutorial, Multi-Threaded Programming II - Thread for Win32 (B), MultiThreading Programming, Synchronization, Resource Sharing, Thread for Win32, Critical Sections, Mutex Regions, Semaphores, Condition Variables, Signaling Event Completion, Thread Debugging, Thread Debugger, Visual Studio 2010"
 name="keywords" />
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1" />
  <link type="text/css" rel="stylesheet" href="../images/bogostyleWidePre.css" />
</head>
<body>
<div id="page" align="center">
<div id="content" style="width: 800px;">
<div id="logo">
<div class="whitetitle" style="margin-top: 70px;">bogotobogo </div>
</div>
<div id="topheader">
<div class="headerbodytext" align="left"><br />
<strong>Bogotobogo</strong><br />
contact@bogotobogo.com </div>
<div class="smallgraytext" id="toplinks"><a href="../index.html">Home</a>
| <a href="../sitemap.html">Sitemap</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="menu">
<div class="smallwhitetext" style="padding: 9px;" align="right"><a
 href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">Products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="submenu">
<div class="smallgraytext" style="padding: 9px;" align="right">
<a href="../gif.html">Gif</a> 
|<a href="../java_applet.html">JavaApplet/Web Start</a>
| <a href="../flash.html">Flash</a>
| <a href="../shockwave.html">ShockWave</a>
| <a href="../svg.html">SVG</a>
| <a href="../iPhone.html">iPhone/iPad</a>
| <a href="../android.html">Android</a>
| <a href="../OnHTML5.html">HTML5</a>
| <a href="../Algorithms/algorithms.html">Algorithms</a>
| <a href="../News/NewsMain.html">News</a>
| <a href="../cplusplus/cpptut.html">C++</a>
| <a href="../Java/tutorial/on_java.html">Java</a>
| <a href="../php/phptut.html">PHP</a>
| <a href="../DesignPatterns/introduction.html">Design Patterns</a>
| <a href="../python/pytut.html">Python</a> 
| <a href="../CSharp/.netframework.html">C#</a>
| <a href="../forums.html">Forums</a> 
| <a href="../VisualBasicSQL/introduction.html">Visual Basic</a>
</div>
</div>

<div id="contenttext">

<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at http://www.google.com/cse/docs/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
  <input type="hidden" name="cref" value="" />
  <input type="hidden" name="ie" value="utf-8" />
  <input type="hidden" name="hl" value="" />
  <input name="q" type="text" size="40" />
  <input type="submit" name="sa" value="Search" />
</form>
<script type="text/javascript" src="http://www.google.com/cse/tools/onthefly?form=searchbox_demo&lang="></script>

<div id="bookmarkshare">
<script type="text/javascript">var addthis_config = {"data_track_clickback":true};</script>
<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=khhong7"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=khhong7"></script>
</div>
<br />
<br />
<br />
<br />
<div style="padding: 10px;"><span class="titletext">C++ Tutorial<br />
Multi-Threaded Programming II<br />
Native Thread for Win32 (B)<br />
(Synchronization and Resource Sharing)</span></div>
<img src="../images/cplusplus/cpp_logo.jpg" alt="cplusplus logo"/>

<div class="bodytext" style="padding: 12px;" align="justify"> 
<div class="subtitle_2nd" id="FullList">List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>   
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL) I</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>
</ul>
</div>
<br />
<img src="images/multithread2B/MyungJeeSan1.png" alt="MyungJeeSan1"/>
<br />
<br />
<br />
<br />

<div class="bodytext" style="padding: 12px;" align="justify">
<div class="subtitle" id="mth2pb">Multi-Threaded (Multicore)  Programming II<br />
Native Thread for Win32 (B)<br />
(Synchronization and Resource Sharing)</div>
<br />
<br />
<br />


<div class="subtitle_2nd" id="syncbetweenthreads">Synchronization Between Threads</div>
<p>In the following example, two threads are used to calculate all the prime numbers in a given range. It demonstrates a test whether a number is prime number:</p> 
<pre>
#include &lt;Windows.h&gt;
#include &lt;process.h&gt
#include &lt;stdio.h&gt
#include &lt;math.h&gt

volatile int counter = 0;

int isPrime(int n)
{
	for(int i = 2; i < (int)(sqrt((float)n) + 1.0) ; i++) {
		if (n % i == 0) return 0;
	}
	return 1;
}

unsigned int __stdcall mythread(void*) 
{
	char* s;
	<font color="blue">while (counter < 25) {
		int number = counter++;
		s = "No";
		if(isPrime(number)) s = "Yes";
		printf("Thread %d value = %d is prime = %s\n",
			GetCurrentThreadId(), number, s);
	}</font>
	return 0;
}

int main(int argc, char* argv[])
{
	HANDLE myhandleA, myhandleB;

	myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
	myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);

	WaitForSingleObject(myhandleA, INFINITE);
	WaitForSingleObject(myhandleB, INFINITE);

	CloseHandle(myhandleA);
	CloseHandle(myhandleB);

	getchar();

	return 0;
}
</pre>
<p>Output is:</p>
<pre>
Thread 2652 value = 0 is prime = Yes
Thread 2652 value = 2 is prime = Yes
Thread 5972 value = 1 is prime = Yes
Thread 5972 value = 4 is prime = No
Thread 5972 value = 5 is prime = Yes
Thread 5972 value = 6 is prime = No
Thread 5972 value = 7 is prime = Yes
Thread 5972 value = 8 is prime = No
Thread 5972 value = 9 is prime = No
Thread 5972 value = 10 is prime = No
Thread 5972 value = 11 is prime = Yes
Thread 5972 value = 12 is prime = No
Thread 5972 value = 13 is prime = Yes
Thread 5972 value = 14 is prime = No
Thread 5972 value = 15 is prime = No
Thread 5972 value = 16 is prime = No
Thread 2652 value = 3 is prime = Yes
Thread 5972 value = 17 is prime = Yes
Thread 5972 value = 19 is prime = Yes
Thread 5972 value = 20 is prime = No
Thread 5972 value = 21 is prime = No
Thread 5972 value = 22 is prime = No
Thread 5972 value = 23 is prime = Yes
Thread 5972 value = 24 is prime = No
Thread 2652 value = 18 is prime = No
</pre>
<p>In the example, we created two threads which tested numbers. Using two threads, however, would cause a <strong>data race</strong> if both threads accessed the variable <strong>counter</strong> at the same time. So, we may need to protect the increment of the <strong>counter</strong> to avoid data races. In the following sections, we'll investigate several approaches to address this problem.</p>
<br />
<br />

<div class="subtitle_2nd" id="debugA">Taste of Debug</div>
<p>The debugging of multithreaded application is a huge topic, and it will be presented in other chapters of this tutorial. But I'll briefly show how to start debugging using Visual Studio 2010 using the same example from the previous section.</p>
<ol>
	<li>Set a break point and run the example in debug mode.
	</li>
	<li>Debug > Windows > Threads
<br /><br />
<img src="images/multithread2B/ThreadDebugWindowOpen.png" alt="ThreadDebugWindowOpen"/>
<br /><br />
	</li>
	<li>Then, we get the Threads window.
<br /><br />
<img src="images/multithread2B/ThreadsWindow.png" alt="ThreadsWindow"/>
<br /><br />
	</li>
	<li>In the Threads window, you can examine and work with threads in the application that you are debugging.<br />
The Threads window contains a table where each row represents a thread in your application. By default, the table lists all threads in your application, but you can filter the list to show only the threads that interest you. Each column contains a different type of information. You can also hide some columns. If you display all columns, the following information appears, from left to right: 
	<ul>
	<li>The <strong>flag</strong> column, where you can mark a thread to which you want to pay special attention. For information about how to flag a thread.</li>
	<li>The <strong>active thread</strong> column, where a yellow arrow indicates an active thread. An outline of an arrow indicates the thread where execution broke into the debugger.</li>
	<li>The <strong>ID</strong> column, which contains the identification number for each thread.</li>
	<li>The <strong>Managed ID</strong> column, which contains the managed identification numbers for managed threads.</li>
	<li>The application <strong>Category</strong> column, which categorizes threads as user interface threads, remote procedure call handlers, or worker threads. A special category identifies the main thread of the application.</li>
	<li>The <strong>Name</strong> column, which identify each thread by name, if it has one, or as <No Name>.</li>
	<li>The <strong>Location</strong> column, which shows where the thread is running. You can expand this location to show the full call stack for the thread.</li>
	<li>The <strong>Priority</strong> column, which contains the priority or precedence that the system has assigned to each thread.</li>
	<li>The <strong>Affinity Mask</strong> column, which is an advanced column that is usually hidden. This column shows the processor affinity mask for each thread. In a multiprocessor system, the affinity mask determines which processors on which a thread can run.</li>
	<li>The <strong>Suspended</strong> column shows the suspend count. A thread must have a suspend count of zero to execute. The native debugger will not show an increment of the Suspend counter when the <strong>Freeze</strong> command is selected from the context menu, but with managed debugging, the Suspend count will reflect <strong>Freeze</strong> and <strong>Thaw</strong> commands. </li>

	</ul>
	<br />
	<li>While we step through the code, we can see which thread is working actively.
<br /><br />
<img src="images/multithread2B/BreakPoint.png" alt="BreakPoint"/>
<br /><br />
	</li>
	<li>One of the features that thread window offers is the <strong>Thread Call Stack Tooltips</strong>. <br /><br />
<img src="images/multithread2B/threadcallstacktooltips.png" alt="threadcallstacktooltips"/>
<br /><br />
	</li>
	<li>Another features is <strong>Show Threads in Source 
</strong>, which thread is at the break point. 
<br /><br />
<img src="images/multithread2B/ShowThreadsInSourceA.png" alt="ShowThreadsInSourceA"/>
<br /><br />
<br /><br />
<img src="images/multithread2B/ShowThreadsInSourceB.png" alt="ShowThreadsInSourceB"/>
<br /><br />
	</li>
	<li>Around this time, you may already know that there is a <strong>Debug Location Bar</strong>.
<br /><br />
<img src="images/multithread2B/DebugLocationBarMenu.png" alt="DebugLocationBarMenu"/>
<br /><br />
<br /><br />
<img src="images/multithread2B/DebugLocationBar.png" alt="DebugLocationBar"/>
<br /><br />
	</li>
	</li>
</ol>
<br />
<br />

<div class="subtitle_2nd" id="criticalsections">Critical Sections</div>
<p>Win32 provides following types of objects that can be used for thread synchronization:</p>
<ul>
	<li>Mutex</li>
	<li>CRITICAL_SECTION</li>
	<li>Semaphore</li>
	<li>Event</li>
</ul>
<p><strong>Mutex</strong> and CRITICAL_SECTION</strong> objects are Win32 versions of the <strong>lock objects</strong>, while <strong>Win32 Semaphores</strong> are counting semaphores.</p>
<p><a href="multithreaded.html#criticalsection" target="blank">Critical sections</a> are a method of ensuring only a single thread executes a region of code. They are declared <strong>within a process</strong> and are <strong>not resources provided by the kernel</strong>. In other words, critical section is a lock object that can only be used to synchronize threads in the same process. Because they are within the process, access to critical sections is quicker than it would be if access had to be brokered by the kernel. This is one of the defferences: <strong>critical sections do not go down to the kernel level</strong>, but <strong>mutex</strong> does go down to the kernel level.</p>
<p>The following code declares a critical section structure, and then initializes it by calling <strong>InitializeCriticalSection()</strong>. Once the program has finished with it, deletes it by calling <strong>DeleteCriticalSection()</strong>:</p>
<pre>
InitializeCriticalSection(&critical);
DeleteCriticalSection(&critical);
</pre>
<p>When a thread needs to enter the critical section, it calls <strong>EnterCriticalSection()</strong>. If no other thread is in the critical section, the calling thread acquires it and continues execution. If another thread is in the critical section, the calling thread will sleep until the thread executing the critical section leaves it by calling <strong>LeaveCriticalSection()</strong>. A thread calling <strong>LeaveCriticalSection()</strong> must be the owner of the critical section. The thread that calls <strong>EnterCriticalSection()</strong> will not return until it has obtained access to the critical section. There is no concept of a timeout.</p>
<p>The following example demonstrates how we use a critical section to protect access to the <strong>counter</strong> variable:</p>
<pre>
#include &lt;Windows.h&gt;
#include &lt;process.h&gt
#include &lt;stdio.h&gt
#include &lt;math.h&gt

volatile int counter = 0;
<font color="blue">CRITICAL_SECTION critical;</font>

int isPrime(int n)
{
	for(int i = 2; i < (int)(sqrt((float)n) + 1.0) ; i++) {
		if (n % i == 0) return 0;
	}
	return 1;
}

unsigned int __stdcall mythread(void*) 
{
	char* s;
	while (counter < 25) {
		<font color="blue">EnterCriticalSection(&critical);</font>
		int number = counter++;
		<font color="blue">LeaveCriticalSection(&critical);</font>
		s = "No";
		if(isPrime(number)) s = "Yes";
		printf("Thread %d value = %d is prime = %s\n",
			GetCurrentThreadId(), number, s);
	}
	return 0;
}

int main(int argc, char* argv[])
{
	HANDLE myhandleA, myhandleB;

	<font color="blue">InitializeCriticalSection(&critical);</font>

	myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
	myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);

	WaitForSingleObject(myhandleA, INFINITE);
	WaitForSingleObject(myhandleB, INFINITE);

	CloseHandle(myhandleA);
	CloseHandle(myhandleB);

	getchar();

	<font color="blue">DeleteCriticalSection(&critical); </font>

	return 0;
}
</pre>
<p>The output from the run is similar to the results:</p>
<pre>
Thread 5716 value = 0 is prime = Yes
Thread 5716 value = 1 is prime = Yes
Thread 5716 value = 2 is prime = Yes
Thread 5716 value = 3 is prime = Yes
Thread 5716 value = 5 is prime = Yes
Thread 5716 value = 6 is prime = No
Thread 5716 value = 7 is prime = Yes
Thread 5716 value = 8 is prime = No
Thread 5716 value = 9 is prime = No
Thread 5716 value = 10 is prime = No
Thread 5716 value = 11 is prime = Yes
Thread 5716 value = 12 is prime = No
Thread 5716 value = 13 is prime = Yes
Thread 5716 value = 14 is prime = No
Thread 5716 value = 15 is prime = No
Thread 5716 value = 16 is prime = No
Thread 5716 value = 17 is prime = Yes
Thread 5436 value = 4 is prime = No
Thread 5436 value = 19 is prime = Yes
Thread 5436 value = 20 is prime = No
Thread 5436 value = 21 is prime = No
Thread 5436 value = 22 is prime = No
Thread 5716 value = 18 is prime = No
Thread 5716 value = 24 is prime = No
Thread 5436 value = 23 is prime = Yes
</pre>
<p>Putting threads to sleep and waking them up again is time-consuming since it involves entering the kernel. All critical sections should be designed to be as short-lived as possible. It is likely that by the time the thread has been put to sleep, the thread that was in the critical section will already have left it. So, making the waiting thread sleep and then waking it up again is just a waste of time.</p>
<p>We have two alternatives. We can call <strong>TryEnterCriticalSection()</strong>. This will return immediately returning either true if the thread has acquired access to the critical section, or false if another thread is currently in the critical section. </p>
<p>Here is the syntax of the function:</p>
<pre>
BOOL WINAPI TryEnterCriticalSection(
 __inout LPCRITICAL_SECTION lpCriticalSection
 ); 
</pre>
<p>where the <strong>lpCriticalSection</strong> [in, out]  is a pointer to the critical section object.</p>
<p>The code that protects access to the <strong>counter</strong> variable could be written using <strong>TryEnterCriticalSection()</strong> as in the example below:</p>
<pre>
#ifndef _WIN32_WINNT		// Allow use of features specific to Windows XP or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif

#include &lt;Windows.h&gt;
#include &lt;process.h&gt
#include &lt;stdio.h&gt
#include &lt;math.h&gt

volatile int counter = 0;
CRITICAL_SECTION critical;

int isPrime(int n)
{
	for(int i = 2; i < (int)(sqrt((float)n) + 1.0) ; i++) {
		if (n % i == 0) return 0;
	}
	return 1;
}

unsigned int __stdcall mythread(void*) 
{
	char* s;
	while (counter < 25) {
		<font color="blue">while(!TryEnterCriticalSection(&critical)){}</font>
		int number = counter++;
		LeaveCriticalSection(&critical);
		s = "No";
		if(isPrime(number)) s = "Yes";
		printf("Thread %d value = %d is prime = %s\n",
			GetCurrentThreadId(), number, s);
	}
	return 0;
}

int main(int argc, char* argv[])
{
	HANDLE myhandleA, myhandleB;

	InitializeCriticalSection(&critical);

	myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
	myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);

	WaitForSingleObject(myhandleA, INFINITE);
	WaitForSingleObject(myhandleB, INFINITE);

	CloseHandle(myhandleA);
	CloseHandle(myhandleB);

	getchar();

	DeleteCriticalSection(&critical); 

	return 0;
}
</pre>
<p>Output is almost identical to the previous results.</p>

<p>In the code, 
<pre>
while(!TryEnterCriticalSection(&critical)){}
</pre>
the line would cause the process to <strong>spin</strong> continuously until it gets the lock. One of the problems with having a thread spin is that it is potentially depriving other threads of processor time. The primary concern would be the case when the spinning thread stops the other thread, which is currently in the critical section, from getting back onto the processor. So, this style of code is one that should be done very carefully.</p>
<p>The other approach is to have the thread which wants to enter the critical section spin briefly in the hope that the thread currently in the critical section will soon leave. If the other thread leaves the critical section, the <strong>spinning thread</strong> can immediately enter the critical section. Once the thread has spun for a predetermined count, the thread goes to sleep until the other thread eventually leaves the critical section. This approach represents a tradeoff between the immediacy of spinning for access to the critical section and the poor utilization of resources that spinning causes.</p>
<p>Critical sections support this idea of spinning for a short time before sleeping. There are two ways of setting the number of times that a thread calling <strong>EnterCriticalSection()</strong> will spin before it goes to sleep. The critical section can be initialized with the value through the initialization call.</p>
<p><strong>InitializeCriticalSectionAndSpinCount()</strong>, which takes the pointer to the critical section, and the spin count as parameters. Or, once the critical section has been created, the spin count can be set through a call to <strong>SetCriticalSectionSpinCount()</strong>:</p>
<pre>
InitializeCriticalSectionAndSpinCount(&critical, 1000);
SetCriticalSectionSpinCount(&critical, 1000);
</pre>

<br />
<br />


<div class="subtitle_2nd" id="regionsmutexes">Mutex Regions</div>
<p>In general, a <strong>mutex</strong> is a common name for a program object that negotiates mutual exclusion among threads, also called a <strong>lock</strong>.</p>
<p><strong>Mutexes</strong> are <strong>kernel object</strong>, which enables them to be shared between processes. This also means that <strong>mutex-protected sections are heavier weight than critical sections</strong>.</p>
<p>Mutexes are created by calling <strong>CreateMutex()</strong> or <strong>CreateMutexEx()</strong>. The call returns the handle to the newly created mutex object.</p>
<p>Here is the syntax:</p>
<pre>
HANDLE WINAPI CreateMutex(
 __in_opt LPSECURITY_ATTRIBUTES lpMutexAttributes,
 __in BOOL bInitialOwner,
 __in_opt LPCTSTR lpName
 ); 
</pre>
<p><strong>Parameters:</strong><p>
<ol>
	<li>The first parameter to the <strong>CreateMutex()</strong> call is a pointer to the security attributes, or zero if the default security attributes should be used.</li>
	<li>The second parameter is a boolean that indicates if the mutex should be created in the state of being already acquired by the calling thread.</li>
	<li>The final parameter is an optional name for the mutex.</li>
</ol>
<p>The <strong>CreateMutex()</strong> call takes the security attributes: an optional name for the mutex, a flag that has either the value <strong>0</strong> or the value <strong>CREATE_MUTEX_INITIAL_OWNER</strong>, which indicates that the mutex should be created as owned by the calling thread, and a mask that sets the access permissions for the mutex.</p>
<p>Once the application has finished with the mutex, the kernel resources need to be freed by calling <strong>CloseHandle()</strong>. The following example demonstrates the process of creating and releasing a mutex:</p>
<pre>
HANDLE mutex;
mutex = CreateMutex(0, 0, 0);
CloseHandle(mutex);
</pre>
<p>The application should call <strong>WaitForSingleObject()</strong> to acquire the mutex. The function either returns with the mutex acquired or returns after the specified timeout. Once the thread has completed, the section of code protected by the mutex can be released by calling <strong>ReleaseMutex()</strong>. The operations <strong>WaitForSingleObject()</strong> and <strong>ReleaseMutex()</strong> are analogous to <strong>EnterCriticalSection()</strong> and <strong>LeaveCriticalSection()</strong>, respectively. The following code shows how:</p>
<pre>
#include &lt;Windows.h&gt;
#include &lt;process.h&gt
#include &lt;stdio.h&gt
#include &lt;math.h&gt

volatile int counter = 0;
<font color="blue">HANDLE mutex;</font>

int isPrime(int n)
{
	for(int i = 2; i < (int)(sqrt((float)n) + 1.0) ; i++) {
		if (n % i == 0) return 0;
	}
	return 1;
}

unsigned int __stdcall mythread(void*) 
{
	char* s;
	while (counter < 25) {
		<font color="blue">WaitForSingleObject(mutex, INFINITE);</font>
		int number = counter++;
		<font color="blue">ReleaseMutex(mutex);</font>
		s = "No";
		if(isPrime(number)) s = "Yes";
		printf("Thread %d value = %d is prime = %s\n",
			GetCurrentThreadId(), number, s);
	}
	return 0;
}

int main(int argc, char* argv[])
{
	HANDLE myhandleA, myhandleB;

	<font color="blue">mutex = CreateMutex(0, 0, 0);</font>

	myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
	myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)1, 0, 0);

	WaitForSingleObject(myhandleA, INFINITE);
	WaitForSingleObject(myhandleB, INFINITE);

	CloseHandle(myhandleA);
	CloseHandle(myhandleB);

	getchar();

	<font color="blue">CloseHandle(mutex);</font>

	return 0;
}
</pre>
<p>Output is almost identical to the previous results.</p>
<p>As you may have noticed, <strong>mutex</strong> object have the following additional features compared to the <strong>critical section</strong>:</p>
<ul>
	<li>A <strong>timeout</strong> can be specified on the request to access a mutex.</li>
	<li>When the mutex is created, there is an argument that specifies whether the thread that creates the mutex object is to be considered as the initial <strong>owner</strong> of the object.</li>
</ul>
<br />
<br />


<div class="subtitle_2nd" id="semaphores">Semaphores</div>
<p>The <strong>semaphores</strong> are a way of keeping track of a count of numbers as well as a way of communicating resource availability between threads. They can be used as an alternative implementation of a mutex at the simplest level. For a more complex use would be to communicate readiness between multiple threads.</p>
<p>A semaphore can be created by calling <strong>CreateSemaphore()</strong>, which takes four parameters:</p>
<p><strong>syntax:</strong><br />
<pre>
HANDLE WINAPI CreateSemaphore(
 __in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
 __in LONG lInitialCount, 
__in LONG lMaximumCount, 
__in_opt LPCTSTR lpName ); 
</pre>
<p><strong>parameters:</strong><br />
<ul>
<li><strong>lpSemaphoreAttributes</strong> [in, optional] <br />
A pointer to a SECURITY_ATTRIBUTES structure. If this parameter is NULL, the handle cannot be inherited by child processes. <br />
The <strong>lpSecurityDescriptor</strong> member of the structure specifies a security descriptor for the new semaphore. If this parameter is NULL, the semaphore gets a default security descriptor. The ACLs in the default security descriptor for a semaphore come from the primary or impersonation token of the creator.</li>
<li><strong>lInitialCount</strong> [in] <br />
The initial count for the semaphore object. This value must be greater than or equal to zero and less than or equal to <strong>lMaximumCount</strong>. The state of a semaphore is signaled when its count is greater than zero and nonsignaled when it is zero. The count is decreased by one whenever a wait function releases a thread that was waiting for the semaphore. The count is increased by a specified amount by calling the ReleaseSemaphore function.</li>
<li><strong>lMaximumCount</strong> [in] <br />
The maximum count for the semaphore object. This value must be greater than zero.</li>
<li><strong>lpName</strong> [in, optional] <br />
The name of the semaphore object. The name is limited to MAX_PATH characters. Name comparison is case sensitive. <br />
If <strong>lpName</strong> matches the name of an existing named semaphore object, this function requests the <strong>SEMAPHORE_ALL_ACCESS</strong> access right. In this case, the <strong>lInitialCount</strong> and <strong>lMaximumCount</strong> parameters are ignored because they have already been set by the creating process. If the <strong>lpSemaphoreAttributes</strong> parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.<br />
If <strong>lpName</strong> is NULL, the semaphore object is created without a name.
If <strong>lpName</strong> matches the name of an existing event, mutex, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns <strong>ERROR_INVALID_HANDLE</strong>. This occurs because these objects share the same name space.<br />
The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space. The remainder of the name can contain any character except the backslash character (\). Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.</li>
</ul>
<p>The second way of creating a semaphore is through the <strong>CreateSemaphoreEx()</strong> call. This takes the same first four parameters but adds two more:</p>
<p><strong>syntax:</strong><br />
<pre>
HANDLE WINAPI CreateSemaphoreEx( 
__in_opt LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
 __in LONG lInitialCount, 
__in LONG lMaximumCount, 
__in_opt LPCTSTR lpName, 
__reserved DWORD dwFlags, 
__in DWORD dwDesiredAccess ); 
</pre>
<p><strong>parameters:</strong><br />
<ul>
<li><strong>dwFlags </strong><br />
This parameter is reserved and must be 0. </li>
<li><strong>dwDesiredAccess</strong> [in] </br />
The access mask for the semaphore object. Passing <strong>SEMAPHORE_ALL_ACCESS</strong> as this parameter will create a semaphore that can be shared between processes. </li>
</ul>
<p>The last way of getting a handle to a semaphore is to call <strong>OpenSemaphore()</strong>, passing in three parameters:</p>
<p><strong>syntax:</strong><br />
<pre>
HANDLE WINAPI OpenSemaphore(
 __in DWORD dwDesiredAccess,
 __in BOOL bInheritHandle, 
__in LPCTSTR lpName ); 
</pre>
<p><strong>parameters:</strong><br />
<ul>
<li><strong>dwDesiredAccess </strong>[in] <br />
The access to the semaphore object. The function fails if the security descriptor of the specified object does not permit the requested access for the calling process. </li>
<li><strong>bInheritHandle</strong> [in] <br />
If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.</li>
<li><strong>lpName</strong> [in] <br />
The name of the semaphore to be opened. Name comparisons are case sensitive. </li>
</ul>
<p>Semaphores are kernel objects, so the create function will return a handle to the new semaphore. When the application has finished with the semaphore, it should release it by calling <strong>CloseHandle()</strong>. Once there are no outstanding handles to the semaphore, the kernel object is disposed of.</p>
<p>A semaphore can be decremented through a call to one of the wait functions, <strong>WaitForSingleObject()</strong>, which takes the handle of the semaphore and a timeout. The function will either return having decremented the semaphore or return the timeout expires.</p>
<p>In the same way, a semaphore can be incremented  by calling <strong>ReleaseSemaphore()</strong>. This call takes three parameters:</p>
<ul>
	<li>the handle of the semaphore</li>
	<li>the amount to increment the semaphore by</li>
	<li>an optional pointer to a <strong>LONG</strong> variable where the previous value will be written</li>
</ul>
<p>Attempts to increment the semaphore beyond the maximum value that it can hold are ignored. Note that a <strong>semaphore has no concept of ownership</strong>, so it cannot tell whether a thread attempts to increment the semaphore by a greater amount than it was decremented.</p>
<p>The following example demonstrates a semaphore being used as a replacement for a mutex. The semaphore is created to hold a maximum value of 1 and an initial value of 1. Two threads are created, and both threads execute the same code, which increments the variable <strong>count</strong> by 100. The end result of this is that the variable <strong>count</strong> contains 200 when the application ends.</p>
<pre>
#include &lt;Windows.h&gt;
#include &lt;process.h&gt
#include &lt;stdio.h&gt

<font color="blue">HANDLE semaphore;</font>
int <font color="blue">count</font> = 0;

void addCount(int increment)
{
	<font color="blue">WaitForSingleObject</font>(semaphore, INFINITE);
	count += increment;
	<font color="blue">ReleaseSemaphore</font>(semaphore, 1, 0);
}

unsigned int __stdcall mythread(void*) 
{
	for ( int i = 0; i < 50; i++) {
		addCount(2);
	}
	return 0;
}

int main(int argc, char* argv[])
{
	HANDLE myhandleA, myhandleB;

	semaphore = CreateSemaphore(0, 1, 1, 0);

	myhandleA = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);
	myhandleB = (HANDLE)_beginthreadex(0, 0, &mythread, (void*)0, 0, 0);

	WaitForSingleObject(myhandleA, INFINITE);
	WaitForSingleObject(myhandleB, INFINITE);

	CloseHandle(myhandleA);
	CloseHandle(myhandleB);
	
	printf("count = %d\n", count);

	getchar();

	CloseHandle(semaphore);

	return 0;
}
</pre>
<p>Output is:</p>
<pre>
count = 200
</pre>
<br />
<br />


<div class="subtitle_2nd" id="condition">Condition Variables</div>
<p>Condition variables were introduced in <strong>Vista</strong>. They work with either a <strong>critical section</strong> or a <strong>slim reader/writer lock</strong> to allow threads to <strong>sleep</strong> until a condition becomes true.</p>
<p>A thread uses a <strong>condition variable</strong> either by acquiring a slim reader/writer lock and then calling <strong>SleepConditionVariableSRW()</strong> or by entering a critical section and calling <strong>SleepConditionVariableCS()</strong>. When the threads are woken from the sleep call, they will again have acquired either the critical section lock or the reader/writer lock depending on how the condition variable is being used. The first thing that the thread needs to do is to test to determine whether the conditions it is waiting on are true, since it is possible for the thread to be worker when the conditions are become false. If the conditions have not been met, the thread should return sleeping on a condition variable.</p>
<p>The following example demonstrates the usage of a condition variable to mediate a producer-consumer pairing of threads. The producer thread would add items onto a queue. To do this, the thread first needs to enter the critical section where it is safe to manipulate the queue data structure. Once the item has been added to the queue, it is safe to exit the critical section. The number of items originally in the queue is returned by the <strong>addItemToQueue()</strong> function. If there were no items in the queue, then it is possible that other threads are waiting on the condition variable and need to be woken up by the producer thread.</p>
<pre>
/* Note:  This code is not complete. 
It's showing just the usage of condition variable */

#include &lt;Windows.h&gt;
#include &lt;process.h&gt

<font color="blue">CONDITION_VARIABLE CV;</font>
CRITICAL_SECTION CS;

void addItem (int value)
{
	LONG oldQueueLength;
	EnterCriticalSection(&CS);
	oldQueueLength = queueLength;
	<font color="blue">addItemToQueue(value);</font>
	LeaveCriticalSection(&CS);
	if(oldQueueLength == 0) <font color="blue">WakeConditionVariable(&CV);</font>
}

int removeItem ()
{
	int item
	EnterCriticalSection(&CS);
	while(QueueLength == 0) <font color="blue">SleepConditionVariableCS(&CV. &CS, INFINITE);</font>
	item = removeItemFromQueue();
	<font color="blue">addItemFromQueue(value);</font>
	LeaveCriticalSection(&CS);
	return item;
}

int main(int argc, char* argv[])
{
	InitializeCriticalSection(&CS);
	<font color="blue">InitializeConditionVariable(&CV);</font>
	DeleteCriticalSection(&CS);

	return 0;
}
</pre>
<br />
<br />


<div class="subtitle_2nd" id="signalingevent">Events</div>
<p>One thread can signal the occurrence of an activity or event to one or more threads using a Win32 object. An event can be either a manual-reset or auto-reset event. The state of an event is either <strong>signaled</strong> or <strong>nonsignaled</strong>. When an event is created, the initial state (signaled or nonsignaled) and the type (manual-reset or auto-reset) is specified. </p>
<p>When the state of a manual-reset event object is set to signaled, it remains signaled until it is explicitly reset to nonsignaled by the <strong>ResetEvent()</strong> function. Any number of waiting threads, or threads that subsequently begin wait operations for the event object specified can be released while the object's state is signaled until a single waiting thread is released; the system then automatically resets the state to nonsignaled.</p>
<p>In other words, <strong>events</strong> are used to signal the fact that an event has happened to one or more threads. It is possible to use semaphores, mutexes, or condition variables to perform the same task.</p>
<p>The threads waiting for an event to occur will wait on that event object. The thread that completes the task will set the event into the signaled state and the waiting threads are then released. Events can be of two types:</p>
<ul>
	<li><strong>manually reset</strong> event type<br />
	This requires the event to be reset before other threads will once again wait on it.</li>
	<li><strong>automatically reset</strong> event type<br />
	This will reset itself after a single thread has been allowed to pass.</li>
</ul>
<p>Events are kernel objects, so the call to <strong>CreateEvent()</strong> will return a handle. The call requires for parameters:</p>
<p><strong>Syntax:</strong></p>
<pre>
HANDLE WINAPI CreateEvent(
 __in_opt LPSECURITY_ATTRIBUTES lpEventAttributes, 
__in BOOL bManualReset, 
__in BOOL bInitialState, 
__in_opt LPCTSTR lpName ); 
</pre>
<p><strong>Parameters:</strong></p>
<ul>
<li><strong>lpEventAttributes </strong> [in, optional] <br />
A pointer to a SECURITY_ATTRIBUTES structure. If this parameter is NULL, the handle cannot be inherited by child processes. <br />
The <strong>lpSecurityDescriptor</strong> member of the structure specifies a security descriptor for the new event. If <strong>lpEventAttributes</strong>  is NULL, the event gets a default security descriptor. The ACLs in the default security descriptor for an event come from the primary or impersonation token of the creator.  </li>
<li><strong>bManualReset</strong>  [in] 
If this parameter is TRUE, the function creates a manual-reset event object, which requires the use of the <strong>ResetEvent</strong> function to set the event state to nonsignaled. If this parameter is FALSE, the function creates an auto-reset event object, and system automatically resets the event state to nonsignaled after a single waiting thread has been released. </li>
<li><strong>bInitialState </strong> [in] 
If this parameter is TRUE, the initial state of the event object is signaled; otherwise, it is nonsignaled. </li>
<li><strong>lpName</strong>  [in, optional] 
The name of the event object. The name is limited to MAX_PATH</strong>  characters. Name comparison is case sensitive. <br />
If <strong>lpName</strong>  matches the name of an existing named event object, this function requests the <strong>EVENT_ALL_ACCESS</strong>  access right. In this case, the <strong>bManualReset</strong>  and <strong>bInitialState</strong>  parameters are ignored because they have already been set by the creating process. If the <strong>lpEventAttributes</strong>  parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored. <br />
If <strong>lpName</strong>  is NULL, the event object is created without a name.
If <strong>lpName</strong>  matches the name of another kind of object in the same name space (such as an existing semaphore, mutex, waitable timer, job, or file-mapping object), the function fails and the GetLastError function returns <strong>ERROR_INVALID_HANDLE</strong> . This occurs because these objects share the same name space.<br />
The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space. The remainder of the name can contain any character except the backslash character (\). Fast user switching is implemented using Terminal Services sessions. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.</li>
</ul>
<p>Existing events can be opened using a call to <strong>OpenEvent()</strong>. </p>
<p><strong>Syntax:</strong></p>
<pre>
HANDLE WINAPI OpenEvent( 
__in DWORD dwDesiredAccess, 
__in BOOL bInheritHandle, 
__in LPCTSTR lpName ); 
</pre>
<p>This call requires three parameters:</p>
<ol>
	<li>The first parameter is the access permissions. If this is zero, default access permissions are requested.</li>
	<li>The second parameter is a boolean that indicates whether the handle should be inherited by child processes.</li>
	<li>The third parameter is the name of the event.</li>
</ol>
<p>A call to <strong>SetEvent()</strong> places the event into the signaled state. This allows threads waiting on the event using <strong>WaitForSingleObject()</strong> to be released. If the event requires a manual reset to get out of the signaled state, then this can be achieved by calling <strong>ResetEvent()</strong>, which also takes the handle to the event. If the event object resets automatically, then only a single thread will be released before the event is reset.</p>
<p>The following example demonstrates the use of an event object to order two threads. An event object is created by calling <strong>CreateEvent()</strong>. This object requires manual reset and is created in the unsignaled state. Two threads are then created. The first thread executes the routine <strong>myThreadA()</strong> and waits on the event. The second thread executes the routine <strong>myThreadB()</strong>, which prints a message and then signals the event object. The signal allows the first thread to continue execution, and then it prints a second message.</p>

	
<pre>
#include &lt;Windows.h&gt;
#include &lt;process.h&gt
#include &lt;stdio.h&gt;

<font color="blue">HANDLE myEvent; </font>

/* The first thread executes the routine myThreadA() and waits on the event. */
unsigned int __stdcall mythreadA(void* param) 
{
	<font color="blue">WaitForSingleObject(myEvent, INFINITE);</font>
	printf("Thread 1 finished\n");
	return 0;
}

/* The second thread executes the routine myThreadB(), 
which prints a message and then signals the event object. */
unsigned int __stdcall mythreadB(void* param) 
{
	printf("Thread 2 finished\n");

	/* A call to SetEvent() places the event into the signaled state.
	This allows threads waiting on the event 
	using WaitForSingleObject() to be released. */
	<font color="blue">SetEvent(myEvent);</font>

	return 0;
}

int main(int argc, char* argv[])
{
	HANDLE myhandleA, myhandleB;

	/*This object requires manual reset 
	and is created in the unsignaled state */
	<font color="blue">myEvent = CreateEvent(0, 0, 0, 0);</font>

	myhandleA = (HANDLE)_beginthreadex(0, 0, &mythreadA, (void*)0, 0, 0);
	myhandleB = (HANDLE)_beginthreadex(0, 0, &mythreadB, (void*)1, 0, 0);

	WaitForSingleObject(myhandleA, INFINITE);
	WaitForSingleObject(myhandleB, INFINITE);

	CloseHandle(myhandleA);
	CloseHandle(myhandleB);

	<font color="blue">CloseHandle(myEvent);</font>

	getchar();

	return 0;
}
</pre>
<p>Output is:</p>
<pre>
Thread 2 finished
Thread 1 finished
</pre>
<br />
<img src="images/multithread2B/MyungJeeSan2.png" alt="MyungJeeSan2"/>
<br />
<br />

<br /><br />
<div class="subtitle_2nd" id="FullList">List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL) I</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>
</ul>

<br /><br />


<br />


<br />
<br />
<br />


</div>
</div>
<div class="smallgraytext" id="footer"><a href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="#">Contact Us</a>
| Bogotobogo &copy; 2011 | <a target="_blank" href="http://www.bogotobogo.com">Bogotobogo </a>
</div>
</div>
</div>
</body>
</html>
